import { downloadFile, yunNaoRequest } from "./fetch";
import { getDataFromLocalStorage, objToFormdata } from "@src/utils/common";
import { SystemEnum } from '@src/dataFormatConversion/enum'
import { SheetExel } from "@src/entries/options/views/NodeData/type";

const baseUrl = import.meta.env.VITE_APP_YUN_NAO_BASE_URL

export const yunNao = {
  /**云脑-存储数据 */
  datapreservation: async (data: any) => {
    return yunNaoRequest(
      baseUrl + '/ess/forward/netrm/doris/data/save',
      data,
      {
        method: "POST",
        body: JSON.stringify(data),
        credentials: "omit", // 设置集成不需要凭证，不然会报请求跨域
        headers: {
          /**获取存储在本地的云脑token验证 */
          Authorization: SystemEnum.TOKEN_Bearer + " " + await getDataFromLocalStorage('access_token') as any,
        } as HeadersInit
      }
    );
  },
  /**云脑-存储数据 */
  datapreservationV2: async (data: any) => {
    return yunNaoRequest(
      baseUrl + '/ess/forward/netrm/doris/data/save/v2',
      data,
      {
        method: "POST",
        body: JSON.stringify(data),
        credentials: "omit", // 设置集成不需要凭证，不然会报请求跨域
        headers: {
          /**获取存储在本地的云脑token验证 */
          Authorization: SystemEnum.TOKEN_Bearer + " " + await getDataFromLocalStorage('access_token') as any,
        } as HeadersInit
      }
    );
  },
  login: async (data: any) => {
    return yunNaoRequest(
      baseUrl + '/admin/oauthuser/login',
      data,
      {
        method: "POST",
        body: JSON.stringify(data),
        headers:{
          'Content-Type':'application/json'
        } as HeadersInit
      }
    );
  },
    /**云脑-存储数据 */
    sheetExcel: async (data: SheetExel, fileName = '') => {
      return downloadFile(
        baseUrl + '/ess/forward/netrm/hd/excel/file/common/data/multi_sheet/download',
        {
          method: "POST",
          body: JSON.stringify(data),
          // credentials: "omit", // 设置集成不需要凭证，不然会报请求跨域
          headers: {
            /**获取存储在本地的云脑token验证 */
            Authorization: SystemEnum.TOKEN_Bearer + " " + await getDataFromLocalStorage('access_token') as any,
            'octet-stream': 'true'
          } as HeadersInit
        },
        fileName
      );
    },
    saveFile: async (data: Record<string,any>, fileName = '') => {
      const saveData = objToFormdata(data, fileName)
      const token = await getDataFromLocalStorage('access_token') as any
      return yunNaoRequest(
        baseUrl + '/ess/forward/netrm/datachannel/file',
        saveData,
        {
          method: "POST",
          body: saveData,
          headers: {
            /**获取存储在本地的云脑token验证 */
            Authorization: SystemEnum.TOKEN_Bearer + " " + token,
            // 'Content-Type':'multipart/form-data'
          } as HeadersInit
        },
        true
      );
    },
    saveJson: async (data: Record<string,any>) => {
      return yunNaoRequest(
        baseUrl + '/ess/forward/netrm/datachannel',
        data,
        {
          method: "POST",
          body: JSON.stringify(data),
          headers: {
            /**获取存储在本地的云脑token验证 */
            Authorization: SystemEnum.TOKEN_Bearer + " " + await getDataFromLocalStorage('access_token') as any,
            'Content-Type':'application/json'
          } as HeadersInit
        },
        true
      );
    },
};
